//-----------------------------------------------------------------------
// <copyright file="CompanyPresenter.cs" company="Simnova LLC">
//     Copyright (c) Simnova LLC. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Simnova.Collaborate.Presenter
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Data;

    public class CompanyPresenter
    {
        private readonly View.ICompany companyView;
        private AppServer.Company company;
        private int userId;

        public CompanyPresenter(View.ICompany companyView, int userId)
        {
            this.userId = userId;
            this.company = new AppServer.Company();
            this.company.CompanyId = 0;
            this.companyView = companyView;
            this.companyView.CompanyId = 0;
            this.LoadCompanies();
        }

        public void LoadCompanies()
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            Simnova.Collaborate.Presenter.AppServer.Company[] companies = collaborateClient.GetCompaniesByAdministrator(this.userId);
            this.companyView.CompanyList = companies;

            collaborateClient.Close();
        }

        public void LoadCompany(int companyId)
        {
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            this.company = collaborateClient.GetCompany(companyId);
            collaborateClient.Close();
            this.companyView.CompanyId = this.company.CompanyId;
            this.companyView.CompanyName = this.company.Name;
        }

        public void Save()
        {
            // Ensure our project object is in sync with the view.
            this.company.CompanyId = this.companyView.CompanyId;
            this.company.Name = this.companyView.CompanyName;

            // Let the application server take care of saving the object. 
            // (this will happen over the network)
            AppServer.CollaborateServiceClient collaborateClient = new AppServer.CollaborateServiceClient();
            collaborateClient.SaveCompany(this.company, this.userId);
            collaborateClient.Close();
            this.LoadCompanies();
        }
    }
}
